/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.evosoft.facade;

import com.evosoft.model.Rol;
import com.evosoft.service.ServiceFactory;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;

/**
 *
 * @author David
 */
public class RolFacade {
    private static RolFacade instance;

    private RolFacade() {
    }

    public synchronized static RolFacade getInstance() {
        if (instance == null) {
            instance = new RolFacade();
        }

        return instance;
    }
    
    
    public void crearRol(Rol rol) throws Exception {
    EntityManager em = Persistence.createEntityManagerFactory("bienestarPU").createEntityManager();
        try {
            em.getTransaction().begin();
            
        	ServiceFactory.getInstance().getRolService().crearRol(rol, em);
        	
            em.getTransaction().commit();
        } catch (Exception e) {
        	if(em != null) {
        		em.getTransaction().rollback();
        	}
                throw e;
        } finally {
        	if(em != null) {
                    em.clear();
                    em.close();
		}
        }    
    }
    
    
    public List<Rol> obtenerRoles() throws Exception {
        EntityManager em = Persistence.createEntityManagerFactory("bienestarPU").createEntityManager();
        List<Rol> roles = new ArrayList<Rol>();
        try {
            roles = ServiceFactory.getInstance().getRolService().obtenerRoles(em);
        }
        catch(Exception e) {
            throw  e;
        }
        return roles;
        
    }

    public List<Rol> obtenerRolesPorEmpresa(int id) throws Exception {
        EntityManager em = Persistence.createEntityManagerFactory("bienestarPU").createEntityManager();
        List<Rol> roles = new ArrayList<Rol>();
        try {
            roles = ServiceFactory.getInstance().getRolService().obtenerRolesPorEmpresa(id,em);
        }
        catch(Exception e) {
            throw  e;
        }
        return roles;
    }
    
}
